package com.chl.legou.upload.controller;

import com.chl.legou.upload.config.FileDfsUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
//@CrossOrigin    //开启跨域访问
public class FileController {

    @Resource
    private FileDfsUtils fileDfsUtils;

    /**
     *  http://localhost:7010/swagger-ui.html
     *  http://192.168.72.130/group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png
     * @param file
     * @return
     */
    @ApiOperation(value = "上传文件",notes = "测试FastDfs文件上传")
    @RequestMapping(value = "/uploadFile",headers = "content-type=multipart/form-data",method = RequestMethod.POST)
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file){
        String result;
        try {
            String path = fileDfsUtils.upload(file);
            if(!StringUtils.isEmpty(path)){
                result = path;
            }else {
                result = "上传失败";
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "服务异常";
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 文件删除
     * @param filePathName
     * @return
     */
    @RequestMapping(value = "/deleteByPath",method = RequestMethod.GET)
    public ResponseEntity<String> deleteByPath(@RequestParam("file") String filePathName){
        // String filePathName = "group1/M00/00/00/wKhjZF3WEDmAPSglAABSZAhj0eU111.jpg" ;
        fileDfsUtils.deleteFile(filePathName);
        return ResponseEntity.ok("删除成功");

    }
}
